<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * The User model.
 *
 * @author PyroCMS Dev Team
 * @package PyroCMS\Core\Modules\Users\Models
 */
class User_m extends MY_Model
{
	public function __construct()
	{
		parent::__construct();

		//$this->profile_table = $this->db->dbprefix('profiles');
		$this->_table='ser_users';
	}

	// --------------------------------------------------------------------------

	/**
	 * Get a specified (single) user
	 *
	 * @param array $params
	 *
	 * @return object
	 */
	 /*
	public function get($params)
	{
		if (isset($params['id']))
		{
			$this->db->where('users.id', $params['id']);
		}

		if (isset($params['email']))
		{
			$this->db->where('LOWER('.$this->db->dbprefix('users.email').')', strtolower($params['email']));
		}

		if (isset($params['role']))
		{
			$this->db->where('users.group_id', $params['role']);
		}

		$this->db
			->select($this->profile_table.'.*, users.*')
			->limit(1)
			->join('profiles', 'profiles.user_id = users.id', 'left');

		return $this->db->get('users')->row();
	}

	
	public function get_recent($limit = 10)
	{
		$this->db->order_by('users.created_on', 'desc');
		$this->db->limit($limit);
		return $this->get_all();
	}

	
	public function get_all()
	{
		$this->db
			->select($this->profile_table.'.*, g.description as group_name, users.*')
			->join('groups g', 'g.id = users.group_id')
			->join('profiles', 'profiles.user_id = users.id', 'left')
			->group_by('users.id');

		return parent::get_all();
	}

	public function add($input = array())
	{
		$this->load->helper('date');

		return parent::insert(array(
			'email' => $input->email,
			'password' => $input->password,
			'salt' => $input->salt,
			'role' => empty($input->role) ? 'user' : $input->role,
			'active' => 0,
			'lang' => $this->config->item('default_language'),
			'activation_code' => $input->activation_code,
			'created_on' => now(),
			'last_login' => now(),
			'ip' => $this->input->ip_address()
		));
	}


	public function update_last_login($id)
	{
		$this->db->update('users', array('last_login' => now()), array('id' => $id));
	}

	// --------------------------------------------------------------------------


	public function activate($id)
	{
		return parent::update($id, array('active' => 1, 'activation_code' => ''));
	}

	// --------------------------------------------------------------------------

	public function count_by($params = array())
	{
		$this->db->from($this->_table)
			->join('profiles', 'users.id = profiles.user_id', 'left');

		if ( ! empty($params['active']))
		{
			$params['active'] = $params['active'] === 2 ? 0 : $params['active'];
			$this->db->where('users.active', $params['active']);
		}

		if ( ! empty($params['group_id']))
		{
			$this->db->where('group_id', $params['group_id']);
		}

		if ( ! empty($params['name']))
		{
			$this->db
				->like('users.username', trim($params['name']))
				->or_like('users.email', trim($params['name']))
				->or_like('profiles.first_name', trim($params['name']))
				->or_like('profiles.last_name', trim($params['name']));
		}

		return $this->db->count_all_results();
	}


	public function get_many_by($params = array())
	{
		if ( ! empty($params['active']))
		{
			$params['active'] = $params['active'] === 2 ? 0 : $params['active'];
			$this->db->where('active', $params['active']);
		}

		if ( ! empty($params['group_id']))
		{
			$this->db->where('group_id', $params['group_id']);
		}

		if ( ! empty($params['name']))
		{
			$this->db
				->or_like('users.username', trim($params['name']))
				->or_like('users.email', trim($params['name']));
		}

		return $this->get_all();
	}*/

	public function get_many_by($param = array()){
		if(!empty($param['not_username'])){
			$this->db->where_not_in('username',$param['not_username']);
		}
		if(!empty($param['group'])){
			$this->db->where('group_id',$param['group']);
		}
		if(!empty($param['room'])){
			$this->db->where('room_id',$param['room']);
		}
		
		
		$active = !empty($param['active']) ? $param['active'] : 0;
		$this->db->where('active',$active);
		//parent::get_all();
		//echo $this->db->last_query();
		return parent::get_all();
	}
	public function get_many_by_join($param = array()){
		$this->db->select('u.id,u.username, u.display_name, u.group_id, u.active, p.room_id, p.salary,p.salary2, p.factor, p.s1,p.s2');
		$this->db->join('emp_profile p','u.id = p.id');
		if(!empty($param['not_username'])){
			$this->db->where_not_in('u.username',$param['not_username']);
		}
		if(!empty($param['group'])){
			$this->db->where('u.group_id',$param['group']);
		}
		if(!empty($param['room'])){
			$this->db->where('p.room_id',$param['room']);
		}
		if(!empty($param['status'])){
			$this->db->where('p.status',$param['status']);
		}
		else{
			$this->db->where('p.status',0);//khong co hop dong
		}
		$active = !empty($param['active']) ? $param['active'] : 0;
		$this->db->where('u.active',$active);
		$this->_table = 'ser_users u';
		return parent::get_all();
	}
	public function get_all($param = array())
	{
		if(!empty($param['not_username']))
			$this->db->where_not_in('username',$param['not_username']);
		return parent::get_all();
	}
	
	public function insert($data){
		$data['password'] = md5($data['password']);
		if(empty($data['active']))
			$data['active'] = 1;
		return parent::insert($data);
	}	
	public function update($id,$data){
		$profile = array();
		$profile['room_id'] = $data['room_id'] ;
		unset($data['room_id']);
		if(!empty($data['password']) ) 
			$data['password'] = md5($data['password']);
		if(empty($data['active']))
			$data['active'] = 0;
		parent::update($id,$data);
		//profile
		
		$this->_table = 'emp_profile';
		parent::update($id,$profile);
	}
	public function get($id){
		$result = parent::get($id);
		//profile
		$this->load->model('emp_profile/profile_m');
		$profile = $this->profile_m->get($id);
		$result = (object)array_merge((array)$result,(array)$profile);
		//plus profile
		return $result;
	}
	//LOGIN
	function login( $username, $password ){
		$this->db->where('u.username',$username);
		$this->db->where('u.password',md5($password));
		$this->db->where('u.active',1);
		$this->db->select('u.*,g.name as `group` ,g.group_by');
		$this->db->join('ser_groups g','g.id = u.group_id');
		return $this->db->get($this->_table.' u')->row();
	}
	
	public function active_select($array_id = array()){
		$str_id = join(',',$array_id);
		//echo $str_id;
		$this->db->where_in('id',$array_id);
		$this->db->update($this->_table,array('active'=>1));
	}
	
	public function delete($id){
		parent::delete($id);
		$this->_table = 'emp_profile';
		parent::delete($id);
	}
	
	public function update_user($id,$data){
		parent::update($id,$data);
	}
}